<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>注册事件的兼容函数</title>
  </head>
  <body>
    <input id="btn" type="button" value="注册事件" />
    <script>
      //注册点击事件
      var elem = document.getElementById("btn");
      function bindEvent(el, eventType, fn) {
        // console.dir(el);
        //IE9以上
        if (el.addEventListener) {
          el.addEventListener(eventType, fn);
          //   console.log("addEventListener");
          //IE9以下
        } else if (el.attachEvent) {
          el.attachEvent("on" + eventType, fn);
          //   console.log("attachEvent");
        } else {
          el["on" + eventType] = fn;
          //   console.log("on");
        }
      }
      function handle() {
        alert("绑定点击事件成功");
      }
      bindEvent(elem, "click", handle);
    </script>
  </body>
</html>
